import type { ZhiHuElements } from '../../../../../../types/constants'
import type { ThemeFutureOptions, ThemeType } from '../../../../../../types/interfaces'
import { BlogThemeService } from '../../../../../../lib/abstract'
import { ARTICLE_LINK_RULES } from '../../../../../../module/page_enhancer/base/element_adapt'
import { DEFAULT_COPY_BUTTON_ID } from '../../../../../../module/page_enhancer/content/copy_protection'
import { BODY_SELECTOR, generateHideSelector, PLATFORM_PAGE_TYPES, PROJECT_PREFIX, SUPPORTED_PLATFORMS } from '../../../../../../types/constants'
import { PAGE_CONTENT_WIDTH } from '../../../../../../types/constants/offical'

export class ZhiHuThemeService extends BlogThemeService {
  readonly name = 'zhiHuTheme'
  readonly platformPageType: string = PLATFORM_PAGE_TYPES.BLOG.value
  private static instance: ZhiHuThemeService
  readonly futureOptions: ThemeFutureOptions = {
    followButton: true,
    options: {
      followButtonClassName: 'FollowButton',
    },
  }

  readonly articleSelector = '#content > div'
  declare readonly elements: ZhiHuElements
  readonly wrapperSelector: string = `${BODY_SELECTOR} #root .Post-Row-Content`
  readonly pageWidth: string = PAGE_CONTENT_WIDTH

  constructor() {
    super(SUPPORTED_PLATFORMS.ZHI_HU)

    this.checkEnv()
    this.initExcludedTitleElementsSelectors(this.excludedTitleElementsSelectors)
    this.updateElementStylesForTheme()
  }

  /**
   * 获取单例实例
   */
  static getInstance(): ZhiHuThemeService {
    if (!ZhiHuThemeService.instance) {
      ZhiHuThemeService.instance = new ZhiHuThemeService()
    }
    return ZhiHuThemeService.instance
  }

  updateElementStylesForTheme(): void {
  }

  platformStyleVar(theme: ThemeType): string {
    return `
/* 购买链接 */
--${PROJECT_PREFIX}-buy-link-bg: ${this.getColorValue(theme, 'buyLinkBg')};
--${PROJECT_PREFIX}-buy-link-shadow: ${this.getColorValue(theme, 'buyLinkShadow')};
--${PROJECT_PREFIX}-buy-link-hover-bg: ${this.getColorValue(theme, 'buyLinkHoverBg')};
/* 专栏卡片 */    
--${PROJECT_PREFIX}-column-card-bg: ${this.getColorValue(theme, 'columnCardBg')};
`
  }

  getPageStyles(_theme: ThemeType): string {
    return `
${BODY_SELECTOR} {
  background-color: var(--${PROJECT_PREFIX}-theme-bg-base) !important;
}
/* 没有页眉时移除文章上边距 */
${BODY_SELECTOR}:has(${generateHideSelector('header')}) {
  div:has(> header) + div:empty {
    display: none !important;
  }
  
  .Post-Row-Content {
    margin-top: 0 !important;
  }
}
`
  }

  getMainStyles(theme: ThemeType): string {
    const { CONTENT_CONTAINER } = this.elements

    let contentContainerStyle = ''
    if (CONTENT_CONTAINER && CONTENT_CONTAINER.selector !== '') {
      contentContainerStyle = `
${CONTENT_CONTAINER.selector} {
  width: ${this.pageWidth} !important;
  margin: 0 auto !important;
}
`
    }

    return `
${contentContainerStyle}

/* 关注按钮 */
${this.getFollowButtonStyles(theme)}

/* 推荐卡片 */
.CPSCommonCard, .ecommerce-ad-arrow[data-ad-type="ecommerce-ad-card"] {
  background: var(--${PROJECT_PREFIX}-buy-link-bg) !important;
  box-shadow: var(--${PROJECT_PREFIX}-buy-link-shadow) !important;
  
  &:hover {
    background: var(--${PROJECT_PREFIX}-buy-link-hover-bg) !important;
  }
}

/* 推荐标题 */
.CPSCommonCard .CPSCommonCard-title, .ecommerce-ad-arrow[data-ad-type="ecommerce-ad-card"] .ecommerce-ad-arrow-main-content-des span {
  color: var(--${PROJECT_PREFIX}-theme-text-secondary) !important;
}

/* 专栏卡片 */
[data-page-element="column-card"]{
  & > div {
    background-color: var(--${PROJECT_PREFIX}-column-card-bg) !important;
  }
  & .column-card-hot-content {
    color: var(--${PROJECT_PREFIX}-theme-text-secondary) !important;
  }
  & .column-card-author {
    color: var(--${PROJECT_PREFIX}-theme-text-primary) !important;
  }
}
`
  }

  getPatchedStyles(_theme: ThemeType): string {
    return ``
  }

  getFixedStyles(_theme: ThemeType): string {
    return `
article {
  margin-top: 10px !important;
}
`
  }

  customPreInnerStyles(_theme: ThemeType): string {
    return `
.code-block-extension-lang, #${DEFAULT_COPY_BUTTON_ID} {
  color: var(--${PROJECT_PREFIX}-theme-code-text-common) !important;
}
`
  }

  LinkIconStyles(theme: ThemeType) {
    return `
${ARTICLE_LINK_RULES.map(rule => `
  &[data-link-type="${rule.id}"]:not(:has(.LinkCard-desc)), 
  &[data-link-type="${rule.id}"] .LinkCard-desc {
    padding-left: 20px !important;
    margin-left: 4px !important;
    position: relative !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 16px;
      height: 16px;
      background-image: var(${this.themeVarService.generateIcon(rule.id)});
      background-size: contain;
      background-repeat: no-repeat;
      ${theme === 'dark' ? 'opacity: 0.85;' : ''}
    }
  }`).join('\n')} 
`
  }

  customLinkStyles(_theme: ThemeType) {
    return `
&.LinkCard {
  background: var(--${PROJECT_PREFIX}-theme-link-card-bg) !important;
  box-shadow: var(--${PROJECT_PREFIX}-theme-link-card-shadow) !important;
  background-blend-mode: overlay;
  border: none;
  border-bottom: none !important;
  
  &:hover {
    transform: translateY(-2px);
  }
}

/* 商品推荐卡片 */
&.GoodsRecommendCard {
  border-bottom: none !important;
  background-color: transparent !important;
}

/* 卡片标题 */
& .LinkCard-title {
  color: var(--${PROJECT_PREFIX}-theme-link-card-text) !important;
}
`
  }

  customImageStyles(theme: ThemeType): string {
    const { CONTENT_CONTAINER } = this.elements

    let coverStyle = ''
    if (CONTENT_CONTAINER && CONTENT_CONTAINER.selector !== '' && theme === 'dark') {
      coverStyle = `
${CONTENT_CONTAINER.selector} img {
  /* 调整封面图片 */
  filter: brightness(0.8) contrast(1.0);
}
`
    }

    return coverStyle
  }
}
